package com.kedou.app;

import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.kedou.adapter.GridAdapter;
import com.kedou.domain.User;
import com.kedou.net.PostFunderList;
import com.kedou.util.NetworkUtil;
import com.kedou.util.PreferencesUtil;
import com.kedou.util.Util;

/**
 * 投资人列表 界面
 * 
 * @author zhouchao
 * 
 */
public class InvestorListActivity extends BaseActivity {
	/**
	 * 中间 Title
	 */
	private TextView mTvTitle;
	/**
	 * 右侧 设置
	 */
	private ImageView mImgSetting;
	/**
	 * 投资人头像 九宫格
	 */
	private GridView mGvInvestor;
	/**
	 * 下拉刷新 上拉加载 附着的 View
	 */
	private PullToRefreshGridView mPullRefreshGridView;
	private GridView mGridView;
	private GridAdapter mAdapter;
	private ArrayList<User> mUsers;
	private int page = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_investorlist);
		initView();
		initGridData();
		setListener();
		getFirstDataFromServer();
	}

	/**
	 * 初始化View
	 */
	private void initView() {
		mTvTitle = (TextView) findViewById(R.id.title);
		mTvTitle.setText(R.string.app_name);
		mImgSetting = (ImageView) findViewById(R.id.img_setting);
		mImgSetting.setVisibility(View.VISIBLE);
		mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
		mGridView = mPullRefreshGridView.getRefreshableView();
	}

	/**
	 * 初始化Gridview数据
	 */
	private void initGridData() {
		mUsers = new ArrayList<User>();
		mAdapter = new GridAdapter(InvestorListActivity.this, mUsers);
		mGridView.setAdapter(mAdapter);
	}

	/**
	 * 设置监听器
	 */
	private void setListener() {
		mPullRefreshGridView
				.setOnRefreshListener(new OnRefreshListener2<GridView>() {

					@Override
					public void onPullDownToRefresh(
							PullToRefreshBase<GridView> refreshView) {

					}

					@Override
					public void onPullUpToRefresh(
							PullToRefreshBase<GridView> refreshView) {
						getMoreDataFromServer();
					}

				});
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				System.out.println(arg2);
				User user = mAdapter.getList().get(arg2);
				Intent intent = new Intent(InvestorListActivity.this,
						InvestorActivity.class);
				intent.putExtra("funderId", user.getUserId());
				InvestorListActivity.this.startActivity(intent);
			}
		});
	}

	/**
	 * 从服务端获取第一次数据
	 */
	protected void getFirstDataFromServer() {
		mProgressDialog.show();
		if (NetworkUtil.isNetworkAvailable(InvestorListActivity.this)) {
			new PostFunderList(PreferencesUtil.getString(this, "token"),
					PreferencesUtil.getString(this, "userId"), 1,
					Config.SOURCE, Util.getImei(this),
					new PostFunderList.SuccessCallback() {

						@Override
						public void onSuccess(String msg, ArrayList<User> mUsers) {
							mProgressDialog.dismiss();
							mAdapter.addList(mUsers);
						}

					}, new PostFunderList.FailCallback() {

						@Override
						public void onFail(String msg) {
							mProgressDialog.dismiss();
							Toast.makeText(InvestorListActivity.this, msg,
									Toast.LENGTH_SHORT).show();
						}
					}, InvestorListActivity.this);
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.check_your_network),
					Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 请求服务端 上拉加载更多
	 */
	protected void getMoreDataFromServer() {
		// mProgressDialog.show();
		page++;
		if (NetworkUtil.isNetworkAvailable(InvestorListActivity.this)) {
			new PostFunderList(PreferencesUtil.getString(this, "token"),
					PreferencesUtil.getString(this, "userId"), page,
					Config.SOURCE, Util.getImei(this),
					new PostFunderList.SuccessCallback() {

						@Override
						public void onSuccess(String msg, ArrayList<User> mUsers) {
							// mProgressDialog.dismiss();
							mAdapter.addList(mUsers);
							mPullRefreshGridView.onRefreshComplete();
						}

					}, new PostFunderList.FailCallback() {

						@Override
						public void onFail(String msg) {
							// mProgressDialog.dismiss();
							Toast.makeText(InvestorListActivity.this, msg,
									Toast.LENGTH_SHORT).show();
						}
					}, InvestorListActivity.this);
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.check_your_network),
					Toast.LENGTH_SHORT).show();
		}
	}
}